package dao;

import entity.Subject;

import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */

public class SubjectDao {
    public int add(Subject s){
        return 0;
    }
    public int update(Subject s){
        return 0;
    }
    public int del(long id){
        return 0;
    }
    //查询全部课程信息
    public List<Subject> findALL(){
        List<Subject>datas=new LinkedList<>();
        //
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection= DriverManager.getConnection(url,"root","root1234");
            ps=connection.prepareStatement("select * from s_subject order by id desc");
            rs=ps.executeQuery();
            while (rs.next()){
                Subject su=new Subject();
                su.setId(rs.getLong("id"));
                su.setSubjectname(rs.getString("subjectname"));
                su.setClasshour(rs.getInt("classhour"));
                su.setGradeId(rs.getLong("gradeid"));
                datas.add(su);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return datas;
    }
    //根据主键找单个对象的查询方法
    public Subject get(long id){
        //
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            connection= DriverManager.getConnection(url,"root","root1234");
            ps=connection.prepareStatement("select * from s_subject where id=?");
            ps.setLong(1,id);
            rs=ps.executeQuery();
            if (rs.next()){
                Subject su=new Subject();
                su.setId(rs.getLong("id"));
                su.setSubjectname(rs.getString("subjectname"));
                su.setClasshour(rs.getInt("classhour"));
                su.setGradeId(rs.getLong("gradeid"));

                return su;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
